package com.fm.common.utils

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.util.TypedValue
import android.widget.CheckBox
import androidx.annotation.*
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fm.common.R
import com.fm.common.wediget.EmptyLayout
import com.fm.common.wediget.SimpleDividerDecoration


/**
 *
 * @Author:         ly2
 * @CreateDate:     2021/1/3
 * @Description:
 *
 */

/**
 * BaseViewHolder 设置checkbox 选中
 * @param viewId
 * @param isChecked 选中状态
 */
fun BaseViewHolder.setChecked(@IdRes viewId: Int, isChecked: Boolean): BaseViewHolder {
    getView<CheckBox>(viewId).isChecked = isChecked
    return this
}

/**
 * BaseViewHolder 加载drawable res
 * @param viewId
 * @param drawableRes 图片res值
 */
fun BaseViewHolder.setImageDrawableRes(@IdRes viewId: Int, @DrawableRes drawableRes: Int): BaseViewHolder {
    getView<AppCompatImageView>(viewId).setImageResource(drawableRes)
    return this
}

/**
 * BaseViewHolder glide 加载图片
 * @param viewId
 * @param url   图片地址
 * @param errorRes  错误时加载图片res
 */
fun BaseViewHolder.glide(@IdRes viewId: Int, url: String?, @DrawableRes errorRes: Int = R.drawable.default_avatar): BaseViewHolder {
    Glide.with(itemView).load(
        if (url.isNullOrEmpty()) {
            errorRes
        } else {
            url
        }
    ).error(errorRes).into(getView<AppCompatImageView>(viewId))
    return this
}

/**
 * RecyclerView 设置LinearLayoutManager
 * @param context
 * @param space 分割间距
 * @param color 分割线颜色
 * @param scrollEnable 是否可滑动
 */
fun RecyclerView.setLinearLayoutManager(context: Context, space: Int = 0, @ColorRes color: Int = R.color.colorF5F5F5, scrollEnable: Boolean = true) {
    val manger = object : LinearLayoutManager(context) {
        override fun canScrollVertically(): Boolean {
            val orientation = orientation
            return if (orientation == VERTICAL) {
                scrollEnable && super.canScrollVertically()
            } else {
                super.canScrollVertically()
            }
        }
    }
    if (space > 0) {
        addItemDecoration(SimpleDividerDecoration(context, color, space))
    }
    layoutManager = manger
}

/**
 * SpannableString 设置颜色
 * @param context
 * @param color 分割线颜色
 * @param start 开始位置
 * @param end 结束位置
 */
fun SpannableString.setColorSpan(context: Context, @ColorRes color: Int, start: Int, end: Int): SpannableString {
    setSpan(ForegroundColorSpan(ContextCompat.getColor(context, color)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
    return this
}

fun SpannableStringBuilder.setColorSpan(context: Context, @ColorRes color: Int, start: Int, end: Int): SpannableStringBuilder {
    setSpan(ForegroundColorSpan(ContextCompat.getColor(context, color)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
    return this
}

/**
 * AppCompatImageView 加载头像
 * @param url 图片地址
 */
fun AppCompatImageView.glideAvatar(url: String? = "") {
    Glide.with(this).load(
        if (url.isNullOrEmpty()) {
            R.drawable.default_avatar
        } else {
            url
        }
    ).error(R.drawable.default_avatar).apply(RequestOptions().circleCrop()).into(this)
}

/**
 * BaseQuickAdapter 设置空布局
 * @param context
 * @param emptyType 布局状态
 * @param bolck     空布局点击触发
 */
fun <T, VH : BaseViewHolder> BaseQuickAdapter<T, VH>.setEmptyView(
    context: Context,
    emptyType: EmptyLayout.EmptyType = EmptyLayout.EmptyType.DEFAULT,
    bolck: (() -> Unit)? = null
) {
    val emptyView = EmptyLayout(context)
    emptyView.setErrorType(emptyType)
    setEmptyView(emptyView)
    emptyLayout?.setOnClickListener {
        bolck?.invoke()
    }
}


/**
 * 保证字符串为空判断 以便后续操作
 */
fun String.emptyParse(): String? {
    return if (this.isEmpty()) {
        null
    } else {
        this
    }
}

/**
 * AppCompatTextView 设置字体大小
 * @param id dimenRes
 */
fun AppCompatTextView.setTextSizeRes(@DimenRes id: Int): AppCompatTextView {
    setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(id))
    return this
}

/**
 * 显示深色模式toast
 */
fun showDarkToast(@StringRes id: Int, isLong: Boolean = false) {
    ToastUtils.make().setDurationIsLong(isLong).setMode(ToastUtils.MODE.DARK).show(id)
}

fun showDarkToast(text: String?, isLong: Boolean = false) {
    if (text.isNullOrEmpty()) {
        return
    }
    ToastUtils.make().setDurationIsLong(isLong).setMode(ToastUtils.MODE.DARK).show(text)
}